package com.taomu.app.zongchouhuli.weight;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;

import java.util.ArrayList;
import java.util.List;

public class PasswordText extends LinearLayout {

    private TextView tv1,tv2,tv3,tv4,tv5,tv6;

    private EditText edit1;

    private List<String> data1s = new ArrayList<>();

    private List<TextView> tv_datas = new ArrayList<>();

    public PasswordText(Context context) {
        this(context,null);
    }

    public PasswordText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,1);
    }

    public PasswordText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.password_layout,this,false);
        tv1 = (TextView) view.findViewById(R.id.one_pwd_tv);
        tv2 = (TextView) view.findViewById(R.id.two_pwd_tv);
        tv3 = (TextView) view.findViewById(R.id.three_pwd_tv);
        tv4 = (TextView) view.findViewById(R.id.four_pwd_tv);
        tv5 = (TextView) view.findViewById(R.id.five_pwd_tv);
        tv6 = (TextView) view.findViewById(R.id.six_pwd_tv);
        edit1 = (EditText) view.findViewById(R.id.edit_view);
        edit1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
        tv_datas.add(tv1);
        tv_datas.add(tv2);
        tv_datas.add(tv3);
        tv_datas.add(tv4);
        tv_datas.add(tv5);
        tv_datas.add(tv6);
        addView(view);
        setTextChangedListener();
    }

    public void inputValue(String value){
        if (data1s.size() >= 6) {

        } else {
            data1s.add("●");
            for (int i = 0; i < data1s.size(); i++) {
                tv_datas.get(i).setText(data1s.get(i));
            }
        }
    }

    public void delete(){
        if(data1s.size()>=1) {
            data1s.remove(data1s.size() - 1);
        }
        for(int i=0;i<tv_datas.size();i++){
            if(i<data1s.size()){
                tv_datas.get(i).setText(data1s.get(i));
            }else{
                tv_datas.get(i).setText("");
            }
        }
    }

    public void clear(){
        data1s.clear();
        for(int i=0;i<tv_datas.size();i++){
            tv_datas.get(i).setText("");
        }
    }

    public EditText getEditText(){
        return edit1;
    }

    public int getDataSize(){
        return data1s.size();
    }

    private String getPwd(){
        StringBuffer buffer = new StringBuffer();
        for(int i=0;i<data1s.size();i++){
            buffer.append(data1s.get(i));
        }
        return buffer.toString();
    }

    private void setInput(String inputstr){
        clear();
        //todo:如果inputstr的长度等于6，输入密码完成做特殊处理
        if(!TextUtils.isEmpty(inputstr)){
            if(inputstr.length() == 6){
                pollSetInputValue(inputstr);
                Log.d("dwqdwqdwqdwq","密码:"+inputstr);
                if(listener != null){
                    listener.onComplete(inputstr);
                }
            }else{
                pollSetInputValue(inputstr);
            }
        }
    }

    private void pollSetInputValue(String inputstr){
        for(int i=0;i<inputstr.length();i++){
            inputValue(new String(new char[]{inputstr.charAt(i)}));
        }
    }

    private void setTextChangedListener(){
        if(edit1 != null){
            edit1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(s != null){
                        String currentstr = s.toString();
                        if(!TextUtils.isEmpty(currentstr)){
                            if(s.length()<=6){
                                setInput(currentstr);
                                Log.d("dwqdqwdwqd","");
                            }
                        }else{
                            clear();
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

    private String getLastStr(String str){
        return  str.substring(str.length()-1,str.length());
    }

    private PasswordTextListener listener;

    public void setPasswordTextListener(PasswordTextListener listener){
        this.listener = listener;
    }

    public interface PasswordTextListener{
        void onComplete(String pwd);
    }
}
